package com.yanqu.road.server.cmd.activity.washe.jinbiao;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.activity.washe.data.WasheUserJinBiaoData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.washe.eWasheModeType;
import com.yanqu.road.logic.bussiness.activity.WasheBusiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.washe.WasheModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

@Cmd(code = GameProtocol.S_WASHE_GET_STAGE_REWARD_FROM_CROSS, desc = "")
public class WasheGetStageRewardFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        long userId = yanQuMessage.getPlayerId();
        WasheProto.ChannelWasheGetStageRewardRespMsg respMsg = WasheProto.ChannelWasheGetStageRewardRespMsg.parseFrom(yanQuMessage.getBytes());
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
        WasheConfig config = WasheMgr.getWasheConfig();
        if (config != null && onlinePlayer != null) {
            WasheUserJinBiaoData jinBiaoData = onlinePlayer.getModule(WasheModule.class).getWasheUserJinBiaoData();
            List<Integer> hadRewardStageList = jinBiaoData.getHadRewardStageList();
            // 处理下领奖
            Property property = new Property();
            boolean flag = false;
            for (WasheProto.ChannelWasheJinBiaoScoreStageTemp stageTemp : respMsg.getStageList()) {
                int stage = stageTemp.getStage();
                if (stageTemp.getRankNum() == -1) {
                    continue;
                }
                if (hadRewardStageList.contains(stage)) {
                    continue;
                }
                flag = true;
                hadRewardStageList.add(stage);
                property.addProperty(config.getStageReward(stage));
            }
            WasheProto.WasheJinBiaoScoreGetStageRewardRespMsg.Builder builder = WasheProto.WasheJinBiaoScoreGetStageRewardRespMsg.newBuilder();
            if (!flag) {
                // 没有可领取的阶段
                builder.setRet(GameErrorCode.E_WASHE_JIN_BIAO_SCORE_STAGE_NOT_REWARD);
            } else {
                jinBiaoData.setHadRewardStageList(hadRewardStageList);
                // 获得道具
                onlinePlayer.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Washe, eLogMoneyType.WasheJinBiaoScoreStageReward);
                builder.setRet(0);
                builder.setReward(PropertyHelper.parsePropertyToString(property));
            }
            onlinePlayer.sendPacket(ClientProtocol.U_WASHE_GET_STAGE_REWARD, builder);
        }
    }
}
